﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LogLibrary.Entity;
using SilverPOP.Data.Process.Helpers.LoggerHelper;
using SilverPOP.Library.BusinessEntities;
using SilverPOP.Data.Process.DAL;
using SilverPOP.Library.Helpers;
using Utilities;
using SilverPOP.Data.Process.BLL;
using SilverPOP.Data.Process.Entity;

namespace SilverPOP.Data.Process.OperationUnit
{
    public class AddCustomColumnProcess : DataAndSchemaProcess
    {
        #region Fields And Properties

        private long SilverpopDatabaseId { get; set; }
        private int ProjectId { get; set; }
        #endregion

        #region Constructor

        List<DatabaseSchemaEntity> _fieldList = null;
        public AddCustomColumnProcess(TaskListEntity task)
        {
            _fieldList = BLL.DatabaseSchema.GetDatabaseSchemaByProjectId(task.ProjectId);
            //  _fieldList = _fieldList.Where(x => x.IsWithContactList == true).ToList();
            DAL.DAL.SetConnectionString(task.ServerName, task.DatabaseName, task.Username, task.Password);
            List<ContactListCreationEntity> obj = SilverPOP.Data.Process.BLL.BaseBLL.PopulateContactEntityCreationObjects(task.CampaignId);
            if (obj != null)
            {
                this.ProjectId = task.ProjectId;
                this.SilverpopDatabaseId = obj[0].SilverPOPDatabaseID;
                this.campaignId = task.CampaignId;
            }
        }

        //public AddCustomColumnProcess(int projectId, long silverpopDatabaseId)
        //{
        //    this.ProjectId = projectId;
        //    this.SilverpopDatabaseId = silverpopDatabaseId;

        //}
        #endregion

        public override void ExecuteOperation()
        {
            DAL.DAL.SetDefautConnectionString();

            List<DatabaseSchemaEntity> _fieldList = BLL.DatabaseSchema.GetDatabaseSchemaByProjectId(this.ProjectId);

            if (_fieldList != null && _fieldList.Count > 0)
            {
                List<CustomDBColumn> _customDBColumnList = new List<CustomDBColumn>();

                foreach (DatabaseSchemaEntity field in _fieldList)
                {
                    if (!field.IsDefault)
                    {
                        _customDBColumnList.Add(new CustomDBColumn(field.FieldName, field.FieldType));
                    }
                }

                String result = SilverPOP.Library.Helpers.SilverPopHelpers.AddCustomColumnInSilverPOPDB(this.InstanceSilverPOPAccount, SilverpopDatabaseId, _customDBColumnList);
                Console.WriteLine(result);

                var logInfo = new LogServiceInformation { CustomErrorMessage = result, StackTrace = Environment.StackTrace };
                LoggerHelper.Log(logInfo, typeof(AddCustomColumnProcess), LogType.INFO);
            }
            else
            {
                Console.WriteLine("No Database Field found with ProjectId:." + ProjectId);

                var logInfo = new LogServiceInformation { CustomErrorMessage = "No Database Field found with ProjectId:." + ProjectId, StackTrace = Environment.StackTrace };
                LoggerHelper.Log(logInfo, typeof(AddCustomColumnProcess), LogType.INFO);
            }
        }
    }//end class
}
